package com.albertyjz.yygh.hosp.controller.api;

import com.albertyjz.yygh.common.exception.YyghException;
import com.albertyjz.yygh.common.helper.HttpRequestHelper;
import com.albertyjz.yygh.common.result.Result;
import com.albertyjz.yygh.common.result.ResultCodeEnum;
import com.albertyjz.yygh.common.utils.MD5;
import com.albertyjz.yygh.hosp.service.DepartmentService;
import com.albertyjz.yygh.hosp.service.HospitalService;
import com.albertyjz.yygh.hosp.service.HospitalSetService;
import com.albertyjz.yygh.hosp.service.ScheduleService;
import com.albertyjz.yygh.model.hosp.HospitalSet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理系统获取数据")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation(value = "上传医院接口信息")
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        //获取医院传递过来的签名
        String hospSign=(String)stringObjectMap.get("sign");

        String hoscode = (String) stringObjectMap.get("hoscode");
//        QueryWrapper<HospitalSet> wrapper=new QueryWrapper<>();
//        wrapper.eq("hoscode",hoscode);
//        HospitalSet hospitalSet = hospitalSetService.getOne(wrapper);
        String signKey=hospitalSetService.getSignKey(hoscode);

        //把从数据库拿到的key进行加密，然后进行比对，因为传过来的时候进行了加密
        String encrypt = MD5.encrypt(signKey);
        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中，+会变成空格
        String logoData=(String)stringObjectMap.get("logoData");
        logoData=logoData.replaceAll(" ","+");
        stringObjectMap.put("logoData",logoData);

        hospitalService.save(stringObjectMap);
        return Result.ok();
    }

    @ApiOperation(value = "上传科室接口信息")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        //获取医院传递过来的签名
        String hospSign=(String)stringObjectMap.get("sign");

        String hoscode = (String) stringObjectMap.get("hoscode");
        String signKey=hospitalSetService.getSignKey(hoscode);

        //把从数据库拿到的key进行加密，然后进行比对，因为传过来的时候进行了加密
        String encrypt = MD5.encrypt(signKey);
        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.save(stringObjectMap);
        return Result.ok();
    }

    @ApiOperation(value = "上传排班接口信息")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        scheduleService.save(stringObjectMap);
        return Result.ok();
    }

}
